using System.Collections.Generic;
using StackCalc.Instructions;

namespace StackCalc.Parsing.ParsingUnits
{
	public class OperatorParser : BaseParsingUnit<Operator>
	{
		/// <summary>
		/// Contains all supported binary operators.
		/// </summary>
		private readonly IDictionary<char, OpCode> _operators = new Dictionary<char, OpCode>
		                                                        	{
		                                                        		{'+', OpCode.Add},
		                                                        		{'-', OpCode.Sub},
		                                                        		{'*', OpCode.Mul},
		                                                        		{'/', OpCode.Div},
		                                                        		{'%', OpCode.Mod},
		                                                        		{'&', OpCode.And},
		                                                        		{'|', OpCode.Or},
		                                                        		{'=', OpCode.Eq},
		                                                        		{'<', OpCode.Lt},
		                                                        		{'>', OpCode.Gt}
		                                                        	};

		protected override bool CanParseImplementation(string input)
		{
			// has to start with an operator
			return _operators.ContainsKey(input[0]);
		}

		protected override Operator ParseImplementation(string input)
		{
			return new Operator(_operators[input[0]]);
		}

		protected override string TrunkateImplementation(string input)
		{
			return input.Remove(0, 1);
		}
	}
}
